#coding=utf-8
'''
Created on Dec 10, 2015
@author: DQA wangpeng 
'''
import data.desired_setting

import data.logging_save

import time
from appium import webdriver
import data.config
import data.get_status
import threading
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
import sys
from time import ctime
reload(sys)
sys.setdefaultencoding('utf-8')

import data.logging_save
import data.Create_Log_Folder
import unittest


class guess(unittest.TestCase):
    def setUp(self):
        #startup the huawei m130 app
        self.save_log = data.Create_Log_Folder.Create('xiami_guess_you_like')
        desired_caps = data.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        data.logging_save.logging.info("startup the app already")
        print 'startup the app already'
        
    def tearDown(self):
        self.driver.quit()
        
        
    def test_alert(self):
        u"""测试步骤：进入到"推荐"模块，轮播推荐里面的歌曲，然后返回到主菜单，loop循环10次  测试依据：底部获取到的名字和HTTP获取到的名字是否一致进行判断"""
        #alert times count
        alert_number = 1
        #excute the test case times
        
        playback_times = 1
        success_times = 1
        fail_times = 1
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        print save_pic
        
        while(alert_number <=10 and playback_times <=10):
            try:
                WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                time.sleep(3)
                alert = self.driver.switch_to_alert()
                alert.accept()
                alert_number = alert_number +1
                
                if alert_number == 10:
                    data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                    print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                    self.driver.get_screenshot_as_file(save_pic)
                    savenumber = savenumber + 1
                    self.fail("the network envoiroment is too bad")
                    
            except:
                WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.playback_button_songname())))     
                self.driver.find_element_by_xpath(data.config.Get_xiami_guess_you_like()).click()
                el_button_text_1 = self.driver.find_element_by_xpath(data.config.playback_button_songname())
                #check if the songname match with the http
                button_songname = el_button_text_1.text
                #for logging display
                data.logging_save.logging.info("底部播放器获取到的歌曲名字是："+str(button_songname))
                #for htmltestrunner display
                print time.ctime(),"底部播放器获取到的歌曲名字是：%s"%button_songname
                get_songname_http = data.get_status.songname()
                time.sleep(2)
                data.logging_save.logging.info("通过网络获取到的歌曲名字是:"+str(get_songname_http))
                print time.ctime(),"通过网络获取到的歌曲名字是：%s"%get_songname_http
                time.sleep(5)
                try:
                    self.assertIn(get_songname_http, button_songname) 
                    print ctime(),"经过对比，文件名比对一致：成功次数为 %d 次"%success_times
                    data.logging_save.logging.info("经过对比，文件名比对一致：成功次数为"+str(success_times)+"次")
                    success_times = success_times + 1
                    
                except:
                    data.logging_save.logging.warning("底部获取名字和实际http获取名字不一致")
                    print time.ctime(),"底部获取名字和实际http获取名字不一致"
                    self.driver.get_screenshot_as_file(save_pic)
                    savenumber = savenumber + 1
                    self.fail("the current song name do not match the real songname after click the 'guess you like'")
                
                playback_times  = playback_times + 1
                    
if __name__ == '__main__':
    unittest.main()
    print 'finished'